<template>
  <div class="login-page">
    <div class="login-container">
      <h2 class="login-title">账号登录</h2>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-position="top"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <div class="remember-forgot">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <el-button type="text" class="forgot-password">忘记密码？</el-button>
        </div>

        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>

        <div class="other-login">
          <p class="other-title">其他登录方式</p>
          <div class="social-login">
            <el-button circle class="social-btn github">
              <span class="fab fa-github" />
            </el-button>
            <el-button circle class="social-btn weixin">
              <span class="fab fa-weixin" />
            </el-button>
            <el-button circle class="social-btn qq">
              <span class="fab fa-qq" />
            </el-button>
          </div>
        </div>

        <div class="register-link">
          还没有账号？<el-button type="text" class="register-button">立即注册</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度至少3个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6个字符', trigger: 'blur' }
  ]
}

const loginFormRef = ref()

const handleLogin = async () => {
  if (!loginFormRef.value) return

  loginFormRef.value.validate((valid: boolean) => {
    if (valid) {
      // 模拟登录成功
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error('请正确填写登录信息')
      return false
    }
  })
}
</script>

<style scoped>
.login-page {
  min-height: calc(100vh - 180px);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 20px;
}

.login-container {
  width: 400px;
  background-color: #fff;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  font-size: 24px;
  font-weight: 600;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forgot-password {
  font-size: 14px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.other-login {
  margin-top: 30px;
  text-align: center;
}

.other-title {
  color: #999;
  font-size: 14px;
  margin-bottom: 15px;
  position: relative;
}

.other-title::before,
.other-title::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 20%;
  height: 1px;
  background-color: #e4e4e4;
}

.other-title::before {
  left: 10%;
}

.other-title::after {
  right: 10%;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
}

.social-btn {
  font-size: 20px;
}

.github {
  background-color: #333;
  color: white;
}

.weixin {
  background-color: #07C160;
  color: white;
}

.qq {
  background-color: #12B7F5;
  color: white;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.register-button {
  font-weight: 600;
}

@media (max-width: 480px) {
  .login-container {
    width: 100%;
  }
}
</style>
